//
//  LeftMenuHeaderView.m
//  99SVR
//
//  Created by apple on 16/3/15.
//  Copyright © 2016年 xia zhonglin . All rights reserved.
//

#import "LeftMenuHeaderView.h"
#import "UIImageView+WebCache.h"

#define kImageWidth 85
#define kCircle 50

@interface LeftMenuHeaderView()
{
    UIImageView *_avatarImageView; // 头像
    UILabel *_nameLabel; // 名称
    UILabel *_vipLevel; // vip等级
    UILabel *_lineView;
    UIImageView *imageB;
    UILabel *lblBContent;
    BOOL isEyeOpen;
}

@property (nonatomic,strong) UIView *loginView;
@property (nonatomic,strong) UIView *unLoginView;
@property (nonatomic,strong) UIImageView * eyeImageView;
@property (nonatomic,strong) UILabel *tradeAccountNum;
@property (nonatomic,strong) UILabel *approTradeAccNum;
@property (nonatomic,strong) UILabel *leverAccountNum;
@property (nonatomic,strong) UILabel *approleverAccNum;
@end

@implementation LeftMenuHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = COLOR_Bg_White;
        
        isEyeOpen = YES;
        self.loginView.hidden = YES;
        self.unLoginView.hidden = NO;
    }
    
    return self;
}

#pragma mark - events response
- (void)loginEvent
{
    if (_delegate && [_delegate respondsToSelector:@selector(enterLogin)]) {
        [_delegate enterLogin];
    }
}

- (void)loginDelegate
{
    if (_delegate && [_delegate respondsToSelector:@selector(enterLogin)])
    {
        [_delegate enterLogin];
    }
}

#pragma mark - private methods
- (void)updateInfoLab:(NSString *)bonusStr {
    UserInfo *userInfo = [UserInfo sharedUserInfo];
    UILabel *lblUserInfo = (UILabel *)[_loginView viewWithTag:2];
    
    if (KUserSingleton.bIsLogin) {
    }
}

#pragma mark - event actions
- (void)eyeClickAction:(UIButton *)sender {
    isEyeOpen = !isEyeOpen;
    NSString * imageName = isEyeOpen? @"balance_visible":@"icon_eye_close";
    self.eyeImageView.image = [UIImage imageNamed:imageName];
}

#pragma mark - getter & setter
- (UIView *)loginView
{
    if (_loginView == nil) {
        _loginView = [[UIView alloc] init];
        [self addSubview:_loginView];
        [_loginView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(self);
        }];
        
        UILabel *lblName = [[UILabel alloc] initWithFrame:Rect(0, 0, kScreenWidth, 20)];
        lblName.tag = 1;
        [lblName setFont:[UIFont boldSystemFontOfSize:17]];
        [lblName setTextColor:color_333333];
        [lblName setTextAlignment:NSTextAlignmentLeft];
        [_loginView addSubview:lblName];
        [lblName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_loginView.mas_top).offset(5);
            make.leading.equalTo(_loginView).offset(20);
        }];
        
        UILabel *lblUserInfo = [[UILabel alloc] initWithFrame:CGRectZero];
        lblUserInfo.tag = 2;
        [lblUserInfo setFont:[UIFont boldSystemFontOfSize:12]];
        [lblUserInfo setTextColor:color_333333];
        [lblUserInfo setTextAlignment:NSTextAlignmentLeft];
        [_loginView addSubview:lblUserInfo];
        [lblUserInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lblName.mas_bottom).offset(10);
            make.leading.equalTo(lblName);
        }];
        
        self.eyeImageView = [[UIImageView alloc] init];
        self.eyeImageView.image = [UIImage imageNamed:@"balance_visible"];
        [_loginView addSubview:self.eyeImageView];
        [self.eyeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(lblUserInfo);
            make.trailing.equalTo(_loginView).offset(-20);
            make.width.equalTo(@20);
            make.height.equalTo(@15);
        }];
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(eyeClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [_loginView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.equalTo(self.eyeImageView).offset(-5);
            make.bottom.trailing.equalTo(self.eyeImageView).offset(5);
        }];
        
        //交易账户
        UIView * leftView = [[UIView alloc] init];
        leftView.backgroundColor = UIColorFromRGB(0x2C313C);
        [_loginView addSubview:leftView];
        [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lblUserInfo.mas_bottom).offset(10);
            make.leading.equalTo(lblUserInfo);
            make.bottom.equalTo(_loginView).offset(-10);
        }];
        
        UILabel *tradeAccountTitle = [[UILabel alloc] initWithFrame:CGRectZero];
        [tradeAccountTitle setFont:[UIFont boldSystemFontOfSize:17]];
        [tradeAccountTitle setTextColor:COLOR_Bg_White];
        tradeAccountTitle.text = @"交易账户";
        [tradeAccountTitle setTextAlignment:NSTextAlignmentLeft];
        [leftView addSubview:tradeAccountTitle];
        [tradeAccountTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.equalTo(leftView).offset(15);
        }];
        
        UILabel *tradeAccountSubTitle = [[UILabel alloc] initWithFrame:CGRectZero];
        [tradeAccountSubTitle setFont:[UIFont boldSystemFontOfSize:12]];
        [tradeAccountSubTitle setTextColor:UIColorFromRGB(0x9A9EA7)];
        tradeAccountSubTitle.text = @"净资产折合 (BTC)";
        [tradeAccountSubTitle setTextAlignment:NSTextAlignmentLeft];
        [leftView addSubview:tradeAccountSubTitle];
        [tradeAccountSubTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tradeAccountTitle.mas_bottom).offset(5);
            make.leading.equalTo(tradeAccountTitle);
        }];
        
        self.tradeAccountNum = [[UILabel alloc] initWithFrame:CGRectZero];
        self.tradeAccountNum.minimumScaleFactor = 0.5;
        [self.tradeAccountNum setFont:[UIFont boldSystemFontOfSize:22]];
        [self.tradeAccountNum setTextColor:COLOR_Bg_White];
        [self.tradeAccountNum setTextAlignment:NSTextAlignmentLeft];
        self.tradeAccountNum.text = @"0.00000000";
        [leftView addSubview:self.tradeAccountNum];
        [self.tradeAccountNum mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(tradeAccountTitle);
            make.trailing.equalTo(leftView).offset(-5);
            make.centerY.equalTo(leftView).offset(10.0);
        }];
        
        self.approTradeAccNum = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.approTradeAccNum setFont:[UIFont boldSystemFontOfSize:12]];
        [self.approTradeAccNum setTextColor:UIColorFromRGB(0x9A9EA7)];
        [self.approTradeAccNum setTextAlignment:NSTextAlignmentLeft];
        self.approTradeAccNum.text = @"≈0.00 CNY";
        [leftView addSubview:self.approTradeAccNum];
        [self.approTradeAccNum mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(tradeAccountTitle);
            make.trailing.equalTo(leftView).offset(-5);
            make.bottom.equalTo(leftView).offset(-15.0);
        }];
        
        
        //杠杠账户
        UIView * rightView = [[UIView alloc] init];
        rightView.backgroundColor = UIColorFromRGB(0x4C8BD3);
        [_loginView addSubview:rightView];
        [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(leftView);
            make.trailing.equalTo(self.eyeImageView);
            make.leading.equalTo(leftView.mas_trailing).offset(10);
            make.width.equalTo(leftView.mas_width);
        }];
        
        UILabel *leverAccountTitle = [[UILabel alloc] initWithFrame:CGRectZero];
        [leverAccountTitle setFont:[UIFont boldSystemFontOfSize:17]];
        [leverAccountTitle setTextColor:COLOR_Bg_White];
        leverAccountTitle.text = @"杠杆账户";
        [leverAccountTitle setTextAlignment:NSTextAlignmentLeft];
        [rightView addSubview:leverAccountTitle];
        [leverAccountTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.equalTo(rightView).offset(15);
        }];
        
        UILabel *leverAccountSubTitle = [[UILabel alloc] initWithFrame:CGRectZero];
        [leverAccountSubTitle setFont:[UIFont boldSystemFontOfSize:12]];
        [leverAccountSubTitle setTextColor:UIColorFromRGB(0x9A9EA7)];
        leverAccountSubTitle.text = @"净资产折合 (BTC)";
        [leverAccountSubTitle setTextAlignment:NSTextAlignmentLeft];
        [rightView addSubview:leverAccountSubTitle];
        [leverAccountSubTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(leverAccountTitle.mas_bottom).offset(5);
            make.leading.equalTo(leverAccountTitle);
        }];
        
        self.leverAccountNum = [[UILabel alloc] initWithFrame:CGRectZero];
        self.leverAccountNum.minimumScaleFactor = 0.5;
        [self.leverAccountNum setFont:[UIFont boldSystemFontOfSize:22]];
        [self.leverAccountNum setTextColor:COLOR_Bg_White];
        [self.leverAccountNum setTextAlignment:NSTextAlignmentLeft];
        self.leverAccountNum.text = @"0.00000000";
        [rightView addSubview:self.leverAccountNum];
        [self.leverAccountNum mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(leverAccountTitle);
            make.trailing.equalTo(rightView).offset(-5);
            make.centerY.equalTo(rightView).offset(10.0);
        }];
        
        self.approleverAccNum = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.approleverAccNum setFont:[UIFont boldSystemFontOfSize:12]];
        [self.approleverAccNum setTextColor:UIColorFromRGB(0x9A9EA7)];
        [self.approleverAccNum setTextAlignment:NSTextAlignmentLeft];
        self.approleverAccNum.text = @"≈0.00 CNY";
        [rightView addSubview:self.approleverAccNum];
        [self.approleverAccNum mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(leverAccountTitle);
            make.trailing.equalTo(rightView).offset(-5);
            make.bottom.equalTo(rightView).offset(-15.0);
        }];
        
    }
    return _loginView;
}

- (UIView *)unLoginView {
    if (_unLoginView == nil) {
        _unLoginView = [[UIView alloc] init];
        _unLoginView.backgroundColor = [UIColor clearColor];
        [self addSubview:_unLoginView];
        [_unLoginView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(self);
        }];
        
        UIView * circleLine = [UIView new];
        [_unLoginView addSubview:circleLine];
        [circleLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(kCircle);
            make.leading.equalTo(@20);
            make.centerY.equalTo(_unLoginView);
        }];
        
        _avatarImageView = [[UIImageView alloc] init];
        _avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImageView.layer.cornerRadius = 4;
        _avatarImageView.layer.masksToBounds = YES;
        _avatarImageView.userInteractionEnabled = YES;
        _avatarImageView.image = [UIImage imageNamed:@"account_default_big_logo"];
        [circleLine addSubview:_avatarImageView];
        [_avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.top.bottom.equalTo(circleLine);
        }];
        
        UILabel * loginLabel = [[UILabel alloc] init];
        [loginLabel setFont:[UIFont boldSystemFontOfSize:25]];
        [loginLabel setTextColor:color_333333];
        [loginLabel setTextAlignment:NSTextAlignmentLeft];
        loginLabel.text = @"请登录";
        [_unLoginView addSubview:loginLabel];
        [loginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(circleLine.mas_trailing).offset(10.0);
            make.centerY.equalTo(_unLoginView.mas_centerY).offset(-10);
        }];
        
        UILabel * subTitleLabel = [[UILabel alloc] init];
        [subTitleLabel setFont:[UIFont boldSystemFontOfSize:12]];
        [subTitleLabel setTextColor:color_333333];
        [subTitleLabel setTextAlignment:NSTextAlignmentLeft];
        subTitleLabel.text = @"欢迎来到火币";
        [_unLoginView addSubview:subTitleLabel];
        [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(loginLabel.mas_leading);
            make.top.equalTo(loginLabel.mas_bottom).offset(5);
        }];
        
        UIButton *btnLogin = [UIButton buttonWithType:UIButtonTypeCustom];
        btnLogin.backgroundColor = [UIColor clearColor];
        [btnLogin addTarget:self action:@selector(loginEvent) forControlEvents:UIControlEventTouchUpInside];
        [_unLoginView addSubview:btnLogin];
        [btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.top.bottom.equalTo(_unLoginView);
        }];
    }
    
    return _unLoginView;
}

- (void)setLogin:(BOOL)login
{
    _login = login;
    UserInfo *userInfo = [UserInfo sharedUserInfo];
    if (KUserSingleton.bIsLogin)
    {
        self.unLoginView.hidden = YES;
        self.loginView.hidden = NO;
        UILabel *lblName = (UILabel *)[_loginView viewWithTag:1];
        lblName.text = [NSString stringWithFormat:@"Hi,%@",userInfo.loginName];
        
        UILabel *lblUserInfo = (UILabel *)[_loginView viewWithTag:2];
        lblUserInfo.text = [NSString stringWithFormat:@"UID:%@",userInfo.userId];
//        [lblName setText:[NSString stringWithFormat:@"%@   (账户ID: %d)",userInfo.strName,userInfo.nUserId]];
//
//        if (KUserSingleton.nStatus) {
//            [self updateInfoLab:myBonusStr];
//        }
        
        _avatarImageView.image = [UIImage imageNamed:@"personal_user_head"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            [self requestBonus];
//            [self requestGoldLog];
        });
    }
    else
    {
        self.loginView.hidden = YES;
        self.unLoginView.hidden = NO;
        [_avatarImageView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"personal_user_head"]];
    }
}

@end
